package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.CarInfo;
import com.suyuan.entity.CompanyInfo;
import com.suyuan.service.CarInfoService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


/**
 *  车辆管理控制类
 *
 */
@Controller
@RequestMapping(value = "/admin/carInfo")
public class CarInfoController {

    @Resource(name = "carInfoService")
    private CarInfoService carInfoService;

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/delete" , method = RequestMethod.POST)
    @ResponseBody
    public Object delete(Integer id){

        int code = carInfoService.delete(id);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }else{
            return JsonMessage.error("删除失败");
        }
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping(value = "/add" , method = RequestMethod.GET)
    public String add(ModelMap model){
        //获取所有公司列表
        List<CompanyInfo> companys = carInfoService.companyList();
        model.addAttribute("companys",companys);
        return "car_add";
    }


    /**
     * 保存
     * @return
     */
    @RequestMapping(value = "/save" , method = RequestMethod.POST)
    @ResponseBody
    public Object save(CarInfo carInfo){
        int code = 0;
        try {
            code = carInfoService.add(carInfo);
        } catch (Exception e) {
            code = 0;
        }
        if(code == 1){
            return JsonMessage.success("添加成功");
        }
        return JsonMessage.error("添加失败");
    }

    /**
     * 查询
     */
    @RequestMapping(value = "/list")
    public Object list(String licenseNumber, String compName, Page page , ModelMap model){
        PageInfo info = carInfoService.inquiry(licenseNumber,compName,page);
        model.addAttribute("page",info);
        model.addAttribute("licenseNumber",licenseNumber);
        model.addAttribute("compName",compName);
        return "car_list";
    }

    /**
     * 批量删除
     * @return
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByids(String ids){
        if(StringUtils.isBlank(ids)){
            return null;
        }
        String[] idArr = null;
        if(ids.length() >= 2){
           idArr = ids.split(",");
        }else{
            idArr = new String[]{ ids };
        }
        for(String id : idArr){
            carInfoService.delete(Integer.parseInt(id));
        }
        return null;
    }

    /**
     * 编辑
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer id,ModelMap model){
        CarInfo carInfo = carInfoService.edit(id);
        List<CompanyInfo> companys = carInfoService.companyList();
        model.addAttribute("companys",companys);
        model.addAttribute("carInfo",carInfo);
        return "car_edit";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(CarInfo carInfo){
        int code = carInfoService.update(carInfo);
        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败");
    }


    /*局部时间类型转换器*/
    /*@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }*/

}
